package com.zhao.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.zhao.common.support.Response;
import com.zhao.common.util.UploadUtil;

/**
 * 上传文件服务端
 * 
 * @author zhaoyongfei
 *
 */
@Controller
@RequestMapping("upload")
public class UploadController extends FrontBaseController {

	@Autowired
	UploadUtil uploadUtil;
	
	public static final String path = "upload";

	
	@RequestMapping()
	public String view(){
		return "back/upload";
	}
	
	@RequestMapping(value = "/single", method = RequestMethod.POST)
	public @ResponseBody Response upload(@RequestParam("file") MultipartFile file) {
		return uploadUtil.uloadFile(file, file.getOriginalFilename());
	}

	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	public @ResponseBody List<Response> batchUpload(HttpServletRequest httpServletRequest) {
		List<MultipartFile> files = ((MultipartHttpServletRequest) httpServletRequest).getFiles("file");
		List<Response> responseList = new ArrayList<Response>();
		for (MultipartFile file : files) {
			String name = UUID.randomUUID() + file.getOriginalFilename();
			logger.debug("文件名：" + name);
			Response response = uploadUtil.uloadFile(file, name);
			responseList.add(response);
		}
		return responseList;
	}
}